// ignore_for_file: unrelated_type_equality_checks

import 'dart:ffi';

import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:flutter/src/widgets/placeholder.dart';
import 'package:full_display/common/qxp_color.dart';
import 'package:full_display/common/qxp_style.dart';
import 'package:full_display/page/montage/montage_provider.dart';
import 'package:full_display/unit/asset_utils/asset_utils.dart';
import 'package:full_display/unit/screen/screen_utility.dart';
import 'package:provider/provider.dart';

class MAngleSettingDialogWidget extends StatelessWidget {
  const MAngleSettingDialogWidget({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [ChangeNotifierProvider(create: (_) => MontageProvider())],
      child: const MAngleSettingDialog(),
    );
  }
}

class MAngleSettingDialog extends StatefulWidget {
  const MAngleSettingDialog({super.key});

  @override
  State<MAngleSettingDialog> createState() => _MAngleSettingDialogState();
}

class _MAngleSettingDialogState extends State<MAngleSettingDialog> {
  var _montageProvider = MontageProvider();
  int currentIndex = 0;
  @override
  Widget build(BuildContext context) {
    _montageProvider = context.watch<MontageProvider>();
    return Container(
      width: 320.0.W,
      height: 366.0.W,
      decoration: const BoxDecoration(
        gradient: LinearGradient(
            //渐变位置
            begin: Alignment.topCenter, //右上
            end: Alignment.bottomCenter, //左下
            //渐变颜色[始点颜色, 结束颜色]
            colors: [ColorPlate.app_blue, ColorPlate.sub_blue]),
        borderRadius: BorderRadius.all(Radius.circular(8.0)),
      ),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Padding(
            padding: EdgeInsets.fromLTRB(20.0.W, 20.0.H, 10.0.W, 20.0.H),
            child: Column(
              children: [
                Text(
                  "角度调节",
                  style: StandardTextStyle.textWhite17Style,
                  textAlign: TextAlign.center,
                ),
                Container(
                  height: 20.0.H,
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.start,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Container(
                      padding: EdgeInsets.only(
                          top: (18.0.W + ((currentIndex % 4) * 70.0.W))),
                      child: currentIndex <= 3
                          ? InkWell(
                              onTap: () {},
                              child: Image.asset(
                                height: 24.0.W,
                                width: 24.0.W,
                                AssetUtils.getAsset2Image("m_angle_sub"),
                              ),
                            )
                          : Container(
                              height: 24.0.W,
                              width: 24.0.W,
                            ),
                    ),
                    Container(
                      width: 10.0.W,
                    ),
                    SizedBox(
                      width: 88.0.W,
                      height: 270.0.W,
                      child: GridView.builder(
                        padding: EdgeInsets.zero,
                        scrollDirection: Axis.vertical,
                        physics: const NeverScrollableScrollPhysics(),
                        itemCount: _montageProvider.angleOneList.length > 4
                            ? 4
                            : _montageProvider.angleOneList.length,
                        itemBuilder: (con, index) {
                          return angleItem(
                            _montageProvider.angleOneList[index]["title"],
                            _montageProvider.angleOneList[index]["isSelect"],
                            index,
                          );
                        },
                        gridDelegate:
                            const SliverGridDelegateWithFixedCrossAxisCount(
                          crossAxisCount: 1,
                          crossAxisSpacing: 1,
                          mainAxisSpacing: 10,
                          //宽高比
                          childAspectRatio: 3 / 2,
                        ),
                      ),
                    ),
                    Container(
                      width: 10.0.W,
                    ),
                    Container(
                      padding: EdgeInsets.only(
                          top: (18.0.W + ((currentIndex % 4) * 70.0.W))),
                      child: currentIndex <= 3
                          ? InkWell(
                              onTap: () {},
                              child: Image.asset(
                                height: 24.0.W,
                                width: 24.0.W,
                                AssetUtils.getAsset2Image("m_angle_add"),
                              ),
                            )
                          : InkWell(
                              onTap: () {},
                              child: Image.asset(
                                height: 24.0.W,
                                width: 24.0.W,
                                AssetUtils.getAsset2Image("m_angle_sub"),
                              ),
                            ),
                    ),
                    Container(
                      width: 10.0.W,
                    ),
                    SizedBox(
                      width: 88.0.W,
                      height: 270.0.W,
                      child: _montageProvider.angleOneList.length > 4
                          ? GridView.builder(
                              padding: EdgeInsets.zero,
                              scrollDirection: Axis.vertical,
                              physics: const NeverScrollableScrollPhysics(),
                              shrinkWrap: true,
                              itemCount: 4,
                              itemBuilder: (con, index) {
                                return angleItem(
                                  _montageProvider.angleOneList[index + 4]
                                      ["title"],
                                  _montageProvider.angleOneList[index + 4]
                                      ["isSelect"],
                                  index + 4,
                                );
                              },
                              gridDelegate:
                                  const SliverGridDelegateWithFixedCrossAxisCount(
                                crossAxisCount: 1,
                                crossAxisSpacing: 1,
                                mainAxisSpacing: 10,
                                //宽高比
                                childAspectRatio: 3 / 2,
                              ),
                            )
                          : Container(),
                    ),
                    Container(
                      width: 10.0.W,
                    ),
                    Container(
                      padding: EdgeInsets.only(
                          top: (18.0.W + ((currentIndex % 4) * 70.0.W))),
                      child: currentIndex > 3
                          ? InkWell(
                              onTap: () {},
                              child: Image.asset(
                                height: 24.0.W,
                                width: 24.0.W,
                                AssetUtils.getAsset2Image("m_angle_add"),
                              ),
                            )
                          : Container(
                              height: 24.0.W,
                              width: 24.0.W,
                            ),
                    ),
                  ],
                )
              ],
            ),
          ),
        ],
      ),
    );
  }

  Widget angleItem(String title, bool selState, int index) {
    return InkWell(
      onTap: () {
        print("============${index}");
        setState(() {
          currentIndex = index;
          _montageProvider.changeAnglehSettingState(index);
        });
      },
      child: Container(
        padding: const EdgeInsets.all(10),
        width: 90.0.W,
        height: 60.0.W,
        decoration: BoxDecoration(
          //设置边框
          border: Border.all(color: Colors.white, width: 1),
          //背景颜色
          color: selState == true
              ? ColorPlate.white
              : ColorPlate.backgroundOpacity0,
          //设置圆角
          borderRadius: BorderRadius.circular((5.0.W)),
        ),
        child: Text(title,
            style: selState == true
                ? StandardTextStyle.textBlue17Style
                : StandardTextStyle.textWhite17Style,
            textAlign: TextAlign.center,
            maxLines: 2,
            overflow: TextOverflow.ellipsis),
      ),
    );
  }
}
